package com.iflytek.controller;

import com.iflytek.entity.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yfliu9
 * @version 1.0
 * @description: TODO
 * @date 2021/8/28 15:45
 */
@RestController
@Slf4j
public class ProductController {

    @Value("${server.port}")
    private Integer port;

    /**
     * 模拟简单调用
     * @return
     */
    @GetMapping("/product")
    public String product() throws InterruptedException {
        log.info("进入商品服务....");
        Thread.sleep(3000);
        return "product ok,当前提供服务的端口：" + port;
    }

    /**
     * 零散参数传递模拟（问号传参）
     * @param name
     * @param id
     * @return
     */
    @GetMapping("/test")
    public String test(String name, Integer id) {
        log.info("接收参数：name：{}，id:{}", name, id);
        return "test ok，当前服务ok，端口：" + port;
    }

    /**
     * 零散参数传递（路径传参模拟）
     * @param name
     * @param id
     * @return
     */
    @GetMapping("/path/{name}/{id}")
    public String testPath(@PathVariable("name") String name, @PathVariable("id") Integer id) {
        log.info("pathvariable方式传参：name：{}，id：{}", name, id);
        return "test PathVariable ok,当前port：" + port;
    }

    /**
     * 对象传参
     * @param product
     * @return
     */
    @PostMapping("/object")
    public String testObject(@RequestBody Product product) {
        log.info("商品信息：{}", product);
        return "test object ok,当前端口：" + port;
    }

    /**
     * 测试传参为数组
     * @param ids
     * @return
     */
    @GetMapping("/array")
    public String testArray(@RequestParam("ids") String[] ids) {
        for (String id : ids) {
            log.info("id : {}", id);
        }
        return "array ok,当前端口：" + port;
    }

    /**
     * 测试集合
     * @param ids
     * @return
     */
    @GetMapping("/collection")
    public String testCollection(@RequestParam("list") List<String> ids) {
        ids.forEach(id -> log.info("id : {}", id));
        return "collection ok,当前端口：" + port;
    }
}
